/*
 * calcStorage.java
 *
 * Created on March 26, 2007, 7:11 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package edu.byu.isys413.actions;

import javax.servlet.http.*;
import edu.byu.isys413.group1E.data.*;
import edu.byu.isys413.group1E.handlers.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

/** This class responds to the request from the web UI to calculate the current
 *storage statistics for the current session's member.
 *
 * @author Travis Kulbeth
 */
public class calcStorage implements edu.byu.isys413.web.Action {
    
    /** Creates a new instance of loadCatalog */
    public calcStorage() {
    }
    
    /**
     * Processes method first extracts the needed backup information for the 
     *current session's member, then gets the used space in the BackupData table, 
     *then calculates the available space.  When the calculations are complete, it
     *generates a string containing the needed information.
     * 
     * 
     * @return String the name of the jsp to call
     * @param request contains the HttpServletRequest data
     * @param response contains the HttpServletResponse data
     * @throws java.lang.Exception to catch any errors and forward them to the ui level
     */
    public String process(HttpServletRequest request, HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        //check the size against the member's available backup space
        CustomerBO cust = (CustomerBO)session.getAttribute("customer");
        MembershipBO memb = cust.getCustMembership();
        ArrayList<BackupDataBO> currentBackups = BackupDataDAO.getInstance().readByMember(memb.getMembershipID());
        //find the used backup space and calculate the remaining space
        double usedSpace = 0;
        for(int x=0;x<currentBackups.size();x++){
            usedSpace = usedSpace + currentBackups.get(x).getFileSize();
        }
        double availSpace = (memb.getBuSize() * 1000000000) - usedSpace;

        //if the size is too big for the space, show message and page to add more space
        String message = new String("<br /><ul><li>Total space: " + memb.getBuSize() + 
                " GB</li><li>Currently used: " + (usedSpace/1000000000) + " GB</li><li>Currently Available: " + (availSpace/1000000000) + " GB</li></ul>");
        request.setAttribute("message", message);
        
        return "orderspace.jsp";
    }
}